﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JCPogi.Core;
using System.ComponentModel.DataAnnotations;

namespace JCPogi.Client {
    public partial class MainForm : Form {
        public MainForm() {
            InitializeComponent();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e) {

        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
            MessageBox.Show("Pogi ka JC.", "Ang Poging si JC", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void MainForm_Load(object sender, EventArgs e) {

        }

        private void message_KeyDown(object sender, KeyEventArgs e) {
            int charactersLeft = 160 - message.Text.Length;
            Color labelColor = Color.ForestGreen;
            if (charactersLeft < 80 && charactersLeft >= 10) {
                labelColor = Color.Tomato;
            }
            else if (charactersLeft >= 0 && charactersLeft < 10) {
                labelColor = Color.Red;
            }

            characterCountLabel.ForeColor = labelColor;
            characterCountLabel.Text = charactersLeft.ToString();
        }

        private void btnSendNow_Click(object sender, EventArgs e) {

            var blaster = new SmsBlaster(JCPogiModemConfiguration.Current)
                .ToRecipients(this.recipientList.Text)
                .WithMessage(this.message.Text);

            var errors = blaster.Prepare();
            if (errors.Any()) {
                StringBuilder errorMessage = new StringBuilder("Unable to send due to the following errors:\r\n");
                foreach (var error in errors) {
                    errorMessage.AppendLine(error.ErrorMessage);
                }

                MessageBox.Show(errorMessage.ToString(), "Unable to Send.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                blaster.Dispose();    
                return;
            }

            var sendSmsCommandMonitor = blaster.CreateContext();

            var sendSmsProgressForm = new SendSmsProgressForm(sendSmsCommandMonitor);
            sendSmsProgressForm.ShowDialog(this);
        }

        
    }
}
